/****************************************************************************
 *
 * MODULE:             rs485_driver
 *
 * COMPONENT:          Serial Link to Host
 *
 * VERSION:
 *
 * REVISION:           $$
 *
 * DATED:              $$
 *
 * STATUS:             $State: Exp $
 *
 * AUTHOR:             Lee Mitchell
 *
 * DESCRIPTION:
 *
 * LAST MODIFIED BY:   $Author: nxp29741 $
 *                     $Modtime: $
 *
 *
 ****************************************************************************/

#ifndef  __MLINK_DT_HANDLE_H
#define  __MLINK_DT_HANDLE_H

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include "mdf_err.h"
#include "driver/uart.h"

#if defined __cplusplus
extern "C" {
#endif



/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/
#define DT_MAX_CHANNEL 2

typedef enum {
	DT_CH_0_UART=0,
	DT_CH_1_UART=1,
	DT_CH_UDP=10,
	DT_CH_TCP=11,
}dt_ch_type_t;

typedef int (*dt_recv_handle_t)(const char *data, int size, void *arg);
int dt_default_recv_handle(const char *data, int size, void * arg);

#define DT_CONFIG_DEFAULT(BAUD) {\
		.enable = 1,\
		.baud_rate = (BAUD),\
		.data_bits = UART_DATA_8_BITS,\
		.parity = UART_PARITY_DISABLE,\
		.stop_bits = UART_STOP_BITS_1,\
		.mode = UART_MODE_RS485_HALF_DUPLEX \
	}


/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/

typedef struct dt_channel_store{
	uint8_t enable;
	int baud_rate;
    uint8_t data_bits;       /*!< UART byte size*/
    uint8_t parity;          /*!< UART parity mode*/
    uint8_t stop_bits;       /*!< UART stop bits*/
	uint8_t mode;
}dt_channel_store_t;

typedef struct dt_config{
	bool enable;
	uart_port_t uart_num;
	int baud_rate;
    uart_word_length_t data_bits;       /*!< UART byte size*/
    uart_parity_t parity;               /*!< UART parity mode*/
    uart_stop_bits_t stop_bits;         /*!< UART stop bits*/
	uart_mode_t mode;
	int tx_io_num;
	int rx_io_num;
	int rts_io_num;
	int cts_io_num;
	dt_recv_handle_t recv_handle;
}dt_config_t;

typedef struct {
	bool init;
	int ch;
	char *rx_buf;
	uart_port_t uart_num;
	QueueHandle_t uart_queue;
	TaskHandle_t uart_task;
	dt_recv_handle_t recv_handle;
}dt_handle_t;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/
extern dt_handle_t g_dt_handler[DT_MAX_CHANNEL];
extern uart_port_t g_dt_uart_num[DT_MAX_CHANNEL];

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/
mdf_err_t mlink_dt_init(void);
mdf_err_t mlink_dt_load_store(void);

esp_err_t dt_save_config_info(uint8_t ch, dt_channel_store_t *cfg);
esp_err_t dt_get_config_info(uint8_t ch, dt_channel_store_t *cfg);

esp_err_t dt_uart_config( dt_handle_t *dt_h, dt_config_t *dt_config);
esp_err_t dt_uart_deinit(dt_handle_t *dt_h);
esp_err_t dt_uart_send_data(uint8_t ch, const char * data, size_t size);
esp_err_t dt_uart_send_bytes(uint8_t ch, const char * data, size_t inlen);
esp_err_t dt_udp_send(const char*addr, int port, const char * data, size_t inlen);
esp_err_t dt_tcp_send(const char*addr, int port, const char * data, size_t inlen);

esp_err_t dt_uart_set_enable( uint8_t ch, int value);
esp_err_t dt_uart_get_enable( uint8_t ch, int *value);
esp_err_t dt_uart_set_baud_rate( uint8_t ch, int value);
esp_err_t dt_uart_get_baud_rate( uint8_t ch, int *value);
esp_err_t dt_uart_set_data_bits( uint8_t ch, int value);
esp_err_t dt_uart_get_data_bits( uint8_t ch, int *value);
esp_err_t dt_uart_set_stop_bits( uint8_t ch, int value);
esp_err_t dt_uart_get_stop_bits( uint8_t ch, int *value);
esp_err_t dt_uart_set_parity( uint8_t ch, int value);
esp_err_t dt_uart_get_parity( uint8_t ch, int *value);

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/

#if defined __cplusplus
}
#endif

#endif  /* __MLINK_DT_HANDLE_H */

/****************************************************************************/
/***        END OF FILE                                                   ***/
/****************************************************************************/


